// $Id: interface_table.cpp 2 2009-09-02 08:47:42Z joseph2002 $
/*
 * Copyright 2009 Joseph Wu
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 *
 * For more information, see http://xpcomplus.sourceforge.net
 */

#define XPCOM_SOURCE

#include <xpcom/config.hpp>
#include <xpcom/assert.hpp>
#include <xpcom/interface.hpp>
#include <xpcom/interface_table.hpp>
#include <xpcom/result_code.hpp>
#include <xpcom/uuid.hpp>

namespace xpcom {

result_t XPCOM_FASTCALL
interface_table_query(
        void* pthis, const interface_table_entry* entries,
        const uuid& iid, void** obj) throw()
{
    while (entries->iid) {
        if (iid == *entries->iid) {
            interface* p = reinterpret_cast<interface*>(
                    reinterpret_cast<char*>(pthis) + entries->offset);
            XPCOM_ASSERT(p);

            p->add_ref();
            *obj = p;

            return NS_OK;
        }

        ++entries;
    }

    *obj = 0;
    return NS_ERROR_NO_INTERFACE;
}

} // namespace xpcom
